package com.cn.jinl.service;


import com.cn.jinl.api.IActionPermissionService;
import com.cn.jinl.api.IRoleService;
import com.cn.jinl.dao.RoleMapper;
import com.cn.jinl.domain.Role;
import com.cn.jinl.domain.RoleExample;
import com.cn.jinl.utils.CollectionsUtil;
import com.cn.jinl.utils.CommonUtil;
import com.cn.jinl.utils.Constants;
import com.cn.jinl.utils.QueryForm;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private IActionPermissionService actionPermissionService;

    @Override
    public List<Role> getRoleList(QueryForm queryForm) throws Exception {
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(Constants.NORMAL_STATUS);
        if(StringUtils.isNotBlank(queryForm.getName())){
            criteria.andRoleNameLike("%"+queryForm.getName()+"%");
        }
        if(StringUtils.isNotBlank(queryForm.getStatus())){
            criteria.andStopStatusEqualTo(queryForm.getStatus());
        }
        example.setOrderByClause("seq DESC");
        List<Role> roles = roleMapper.selectByExample(example);
        Map<String,String> map = actionPermissionService.queryActionPermissionMap();
        if(CollectionsUtil.isNotEmpty(roles)){
            for(Role role:roles){
                String permission = role.getRolePermission();
                if(StringUtils.isNotBlank(permission)){
                    String array[] =  permission.split(",");
                    List<String> converts = Lists.newArrayList();
                    for(int i = 0 ; i <array.length;i++ ){
                        String cn = "";
                        String per = array[i];
                        String split[]  = per.split(":");
                        if(split.length == 3){
                            String parentPer = split[0] + ":"+ split[1];
                            cn = map.get(parentPer) +":";
                        }
                        cn += map.get(array[i]);
                        if(StringUtils.isNotBlank(cn)){
                            converts.add(cn);
                        }
                    }
                    role.setRolePermissionCn(Joiner.on(",").join(converts));
                }
            }
        }
        return roles;
    }

    public int countRoleByName(String name,String id){
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andRoleNameEqualTo(name);
        if(StringUtils.isNotBlank(id)){
            criteria.andIdNotEqualTo(id);
        }
        int count = roleMapper.countByExample(example);
        return count;
    }

    @Override
    public String addRoleDetail(Role role) throws Exception {
        if(this.countRoleByName(role.getRoleName(),"") > 0){
            return "角色重复";
        }
        role.setId(CommonUtil.generateRandomNum("role-"));
        int count = roleMapper.insertSelective(role);
        return CommonUtil.outStr(count);
    }

    @Override
    public String updateRoleDetail(Role role) throws Exception {
        String id = role.getId();
        if(StringUtils.isBlank(id)){
            throw new Exception("updateSetRoleDetail id null");
        }
        if(this.countRoleByName(role.getRoleName(),role.getId()) > 0){
            return "角色重复";
        }
        RoleExample example = new RoleExample();
        example.createCriteria().andIdEqualTo(id);
        int count = roleMapper.updateByExampleSelective(role,example);
        return CommonUtil.outStr(count);
    }

    @Override
    public String deleteRoleDetail(QueryForm queryForm) throws Exception {
        List<String> ids = queryForm.getIds();
        if(CollectionsUtil.isEmpty(ids)){
            throw new Exception("deleteSetRoleDetail ids 为空！");
        }
        Role update = new Role();
        update.setStatus(Constants.DEL_STATUS);
        RoleExample example = new RoleExample();
        example.createCriteria().andIdIn(ids);
        int count = roleMapper.updateByExampleSelective(update,example);
        return  CommonUtil.outStr(count);
    }

    @Override
    public List<String> queryRolePermission(String roleId) throws Exception {
        List<String> permissionList = Lists.newArrayList();
        if(StringUtils.isBlank(roleId)){
            return Lists.newArrayList();
        }
        RoleExample example = new RoleExample();
        example.createCriteria().andIdEqualTo(roleId).andStatusEqualTo(Constants.NORMAL_STATUS);
        List<Role> roles = roleMapper.selectByExample(example);
        if(roles.size()> 1){
            throw new Exception("querySetRolePermission 查询出错："+roleId);
        }
        if(CollectionsUtil.isNotEmpty(roles)){
            Role role = roles.get(0);
            String permission = role.getRolePermission();
            if(StringUtils.isNotBlank(permission)){
                String array[] = permission.split(",");
                permissionList = new ArrayList<>(Arrays.asList(array));
            }
        }
        return permissionList;
    }
}
